package com.twu.biblioteca.presenter;

import com.twu.biblioteca.model.IFactory;
import com.twu.biblioteca.model.session.exceptions.InvalidSessionException;
import com.twu.biblioteca.model.session.Session;
import com.twu.biblioteca.model.session.SessionManager;
import com.twu.biblioteca.model.users.*;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 08/08/2013
 * Time: 13:47
 * To change this template use File | Settings | File Templates.
 */
public class MembershipPresenter implements Presenter {

    public interface Display{
        public void prompt();
        public void displayDetails(UserFormatter formatter);
    }

    private final Display view;
    private final SessionManager sessionManager;
    private final UserManager userManager;
    private final UserFormatter userFormatter;

    public MembershipPresenter(SessionManager sessionManager, UserManager userManager, UserFormatter userFormatter, Display view) {
        this.view = view;
        this.sessionManager = sessionManager;
        this.userManager = userManager;
        this.userFormatter = userFormatter;
    }

    @Override
    public void go(Session session) {
        try {
            Username username = sessionManager.validate(session);
            User user = userManager.get(username);

            userFormatter.add(user);
            view.displayDetails(userFormatter);
        } catch (InvalidSessionException e) {
            view.prompt();
        }
    }
}
